<template>
  <div>
    <!-- dialog弹出框 -->
    <el-dialog
      title="审核信息"
      :visible.sync="show"
      width="50%"
      :before-close="closeDialog"
    >
      <!-- from表单 -->
      <div class="from-infomations">
        <el-form
          ref="form"
          label-position="left"
          label-width="5rem"
        >
          <el-form-item label="姓名">
            {{ userInfo.userName }}
          </el-form-item>
          <el-form-item label="工号">
            {{ userInfo.userId }}
          </el-form-item>
          <el-form-item label="活动">
            <el-collapse accordion>
              <el-collapse-item
                v-for="(OneSendact, index) in Sendact"
                :key="index"
                :title="OneSendact.actTitle"
              >
                <el-form-item label="活动内容">
                  {{ OneSendact.actInfo }}
                </el-form-item>
                <el-form-item label="提交时间">
                  {{ OneSendact.actTime }}
                </el-form-item>
                <el-form-item label="活动图片">
                  <div class="img-list">
                    <el-scrollbar>
                      <ul>
                        <li
                          v-for="(img, indexs) in OneSendact.images"
                          :key="indexs"
                        >
                          <el-image
                            :src="img.imageUrl"
                            class="imageInfo"
                            :preview-src-list="lookBigImage(index, indexs)"
                          />
                        </li>
                      </ul>
                    </el-scrollbar>
                  </div>
                </el-form-item>
                <el-form-item label="活动内容">
                  {{ OneSendact.actInfo }}
                </el-form-item>
                <!-- 审核状态 -->
                <el-form-item label="审核状态">
                  <template>
                    <el-radio-group
                      v-model="OneSendact.actStatus"
                    >
                      <el-radio :label="1">审核通过</el-radio>
                      <el-radio :label="2">审核未通过</el-radio>
                    </el-radio-group>
                  </template>
                </el-form-item>
                <el-form-item v-if="OneSendact.actStatus === 1" label="评分">
                  <el-input
                    v-model="OneSendact.actScore"
                    style="width:13rem"
                    placeholder="请输入分数"
                    clearable
                    oninput="value=value.replace(/^\.+|[^\d.-]/g,'')"
                    maxlength="4"
                    show-word-limit
                  />
                </el-form-item>
                <el-form-item v-if="OneSendact.actStatus === 2" label="未过原因">
                  <el-select v-model="OneSendact.unpassInfo" placeholder="请选择">
                    <el-option
                      v-for="item in unpassInfos"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value"
                    />
                  </el-select>
                </el-form-item>
                <el-form-item
                  v-if="OneSendact.actStatus === 2 && OneSendact.unpassInfo === 'other'"
                  label=" "
                >
                  <el-input
                    v-model="OneSendact.unpassEndReason"
                    placeholder="请输入未通过原因"
                    type="textarea"
                    :rows="1"
                    clearable
                    maxlength="20"
                    show-word-limit
                    @input="((val)=>{unpassEndReasonChange(val, index)})"
                  />
                </el-form-item>
              </el-collapse-item>
            </el-collapse>
          </el-form-item>

        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="closeDialog()">取 消</el-button>
        <el-button type="primary" @click="sendDialog()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { getAllInfo } from '../../api/user.js'
import { getSendInfo, getUpdateSendScore1, getUpdateSendStatus, selectActBySendId, UpdateByActId } from '../../api/send.js'
import { getSlist } from '../../api/slist.js'
import { getTlist } from '../../api/tlist.js'
import { addUnPass, delUnPass, updateUnPass } from '../../api/unpass.js'
import { isOpenSendTime } from '../../utils/isOpenSendTime.js'
import Vue from 'vue'
export default {
  props: {
    // 状态获取
    getStatusType: {
      type: Array,
      default: () => {}
    }
  },
  data() {
    return {
      // 获取全部数据存储位置
      show: false,
      sendInfo: {
        sendId: '',
        sendInfo: '',
        sendFlistId: '',
        sendSlistId: '',
        sendTlistId: '',
        sendUserId: '',
        sendStatus: '',
        sendScore: '',
        sendGiveUserId: ''
      },
      userInfo: {
        userId: '',
        userPwd: '',
        userType: '',
        userBranch: '',
        userName: '',
        userReconsider: '',
        userScore: ''
      },
      statusType: {},
      Sendact: [],
      // 代码数据存储位置
      // 是否输入
      IsPass: 0,
      // 整体是否审核通过
      IsExamine: 1,
      sendStatus: '',
      unpassInfos: [
        {
          value: 'SD27001',
          label: '提交位置错误'
        },
        {
          value: 'SD27002',
          label: '标题不合格'
        },
        {
          value: 'SD27003',
          label: '详细信息不合格'
        },
        {
          value: 'SD27004',
          label: '照片不合格'
        },
        {
          value: 'other',
          label: '其他'
        }
      ],
      rules: {
        unpassEndReason: [{ required: true, message: '请输入未通过原因', trigger: 'blur' }]
      }
    }
  },
  watch: {},
  created() {
    // dialog信息初始化
    this.createDialogStatus()
  },
  methods: {
    // 全部调用方法
    // 展示dialog
    unpassEndReasonChange(e, index) {
      var that = this
      that.Sendact[index].unpassEndReason = e
    },
    showDialog(data) {
      this.show = true
      var that = this
      //  获取用户信息
      that.statusType = data
      new Promise(function(resolve, reject) {
        // 根据用户id获取用户信息
        var userId = that.statusType.userId
        getAllInfo(userId).then((res) => {
          resolve(res.obj)
        })
      }).then((data) => {
        that.userInfo.userId = data.userId
        that.userInfo.userType = data.userType
        that.userInfo.userName = data.userinfo.userName
        that.userInfo.userBranch = data.userinfo.userBranch
        that.userInfo.userReconsider = data.userinfo.userReconsider
        that.userInfo.userScore = data.userinfo.userScore
        // 根据sendId判断send表
        var sendId = that.statusType.sendId
        if (sendId === null) {
          // 设置数据，保存当前状态
          that.sendStatus = 0
        } else {
          // 设置数据，保存当前状态
          that.sendStatus = 1
          // send表中有数据，根据sendId查找send表中的信息
          that.findSend(sendId)
          // 获取活动信息
          that.findAct(sendId)
        }
      })
    },
    // 根据sendId查询send表中的信息
    findSend(sendId) {
      var that = this
      new Promise(function(resolve, reject) {
        getSendInfo(sendId).then((res) => {
          resolve(res.obj)
        })
      }).then((data) => {
        that.sendInfo = data
        that.findRouterRules()
      })
    },
    // 获取当前路由分数类型和分数上下限
    findRouterRules() {
      var that = this
      var slist = sessionStorage.getItem('slist')
      var tlist = sessionStorage.getItem('tlist')
      if (tlist === 'nolook') {
        new Promise(function(resolve, reject) {
          getSlist(slist).then((res) => {
            resolve(res.obj)
          })
        }).then((data) => {
          that.routerRules = data
        })
      } else {
        new Promise(function(resolve, reject) {
          getTlist(tlist).then((res) => {
            resolve(res.obj)
          })
        }).then((data) => {
          that.routerRules = data
        })
      }
    },
    // 再次提交，修改信息
    updateSend() {
      var that = this
      that.sendInfo.sendGiveUserId = sessionStorage.getItem('userId')
      var sendInfo = that.sendInfo
      new Promise(function(resolve, reject) {
        getUpdateSendStatus(sendInfo).then((res) => {
          resolve(res.obj)
        })
      }).then((data) => {
        if (data === null) {
          that.show = false
          that.Sendact = []
          that.$message({
            message: '提交成功',
            type: 'success'
          })
          that.$router.go(0)
        }
      })
    },
    // 根据sendId查询所有活动和图片以及所有不通过原因
    findAct(sendId) {
      var that = this
      new Promise(function(resolve, reject) {
        selectActBySendId(sendId).then((res) => {
          resolve(res.obj)
        })
      }).then((data) => {
        for (var i = 0; i < data.length; i++) {
          that.Sendact.push({})
          Vue.set(that.Sendact[i], 'actId', data[i].actId)
          Vue.set(that.Sendact[i], 'actStatus', data[i].actStatus)
          Vue.set(that.Sendact[i], 'actScore', data[i].actScore)
          Vue.set(that.Sendact[i], 'actOldScore', data[i].actScore)
          Vue.set(that.Sendact[i], 'actSendId', data[i].actSendId)
          Vue.set(that.Sendact[i], 'actTitle', data[i].actTitle)
          Vue.set(that.Sendact[i], 'actInfo', data[i].actInfo)
          Vue.set(that.Sendact[i], 'actTime', data[i].actTime)
          Vue.set(that.Sendact[i], 'actUpdateTime', data[i].actUpdateTime)
          Vue.set(that.Sendact[i], 'images', data[i].images)
          Vue.set(that.Sendact[i], 'unpassInfo', null)
          Vue.set(that.Sendact[i], 'unpassId', null)
          Vue.set(that.Sendact[i], 'unpassEndReason', null)
          if (data[i].unpass !== null) {
            if (data[i].actStatus === 1) {
              Vue.set(that.Sendact[i], 'unpassInfo', null)
              Vue.set(that.Sendact[i], 'unpassId', null)
              Vue.set(that.Sendact[i], 'unpassEndReason', null)
            } else if (data[i].actStatus === 2) {
              Vue.set(that.Sendact[i], 'unpassInfo', data[i].unpass.unpassInfo)
              Vue.set(that.Sendact[i], 'unpassId', data[i].unpass.unpassId)
              Vue.set(that.Sendact[i], 'unpassEndReason', data[i].unpass.unpassInfo)
              if (that.Sendact[i].unpassInfo !== 'SD27001' &&
               that.Sendact[i].unpassInfo !== 'SD27002' &&
               that.Sendact[i].unpassInfo !== 'SD27003' &&
               that.Sendact[i].unpassInfo !== 'SD27004') {
                that.Sendact[i].unpassInfo = 'other'
              }
            }
          }
        }
      })
    },
    // 添加未通过原因
    unpassSend(Oneact) {
      new Promise(function(resolve, reject) {
        addUnPass(Oneact).then((res) => {
          resolve(res.obj)
        })
      }).then((data) => {
        // 修改成绩信息

      })
    },
    // 修改未通过原因
    unpassUpdate(Oneact) {
      new Promise(function(resolve, reject) {
        updateUnPass(Oneact).then((res) => {
          resolve(res.obj)
        })
      }).then((data) => {})
    },
    // 删除未通过信息
    unpassDel(Oneact) {
      new Promise(function(resolve, reject) {
        delUnPass(Oneact.unpassId).then((res) => {
          resolve(res.obj)
        })
      }).then((data) => {})
    },
    // 修改活动状态
    UpdateAct(Oneact) {
      new Promise(function(resolve, reject) {
        UpdateByActId(Oneact).then((res) => {
          resolve(res.obj)
        })
      }).then(() => {
        getSendInfo(Oneact.actSendId).then((data) => {
          if (Oneact.actScore !== null) {
            const sendScore = data.obj.sendScore - Oneact.actOldScore + parseInt(Oneact.actScore)
            // 更新send表信息
            getUpdateSendScore1(Oneact.actSendId, sendScore).then()
          }
        })
      })
    },
    // 页面逻辑方法
    // dialog信息初始化
    createDialogStatus() {
      this.show = false
    },

    // 关闭dialog
    closeDialog() {
      this.$confirm('是否确定取消', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.show = false
          this.Sendact = []
          this.$message({
            type: 'success',
            message: '取消审核成功'
          })
        })
        .catch(() => {})
    },
    // 提交dialog
    sendDialog() {
      var that = this

      // 看下是否在开放时间内
      isOpenSendTime().then((res) => {
        that.timeType = res
        if (that.timeType === 1) {
          for (var i = 0; i < that.Sendact.length; i++) {
            if (that.Sendact[i].unpassInfo !== 'other') {
              that.Sendact[i].unpassEndReason = that.Sendact[i].unpassInfo
            }
            if ((
              that.Sendact[i].unpassEndReason === '' ||
              that.Sendact[i].unpassEndReason === null) &&
              that.Sendact[i].actStatus === 2) {
              that.IsPass = 1
              break
            } else {
              that.IsPass = 2
            }
          }
          if (that.IsPass === 1) {
            that.$message({
              message: '请输入未通过原因',
              type: 'warning'
            })
          } else {
            new Promise(function(resolve, reject) {
              that.$confirm('是否确定提交', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
              })
                .then(() => {
                  that.$message('正在提交中')
                  resolve(1)
                })
                .catch(() => {
                  resolve(2)
                })
            })
              .then((data) => {
                if (data === 1) {
                  if (that.getStatusType.sendId === null) {
                    that.$message({
                      message: '当前服务器繁忙，请稍后再试',
                      type: 'warning'
                    })
                  } else if (that.getStatusType.sendId !== null) {
                    for (var j = 0; j < that.Sendact.length; j++) {
                      if (
                        that.Sendact[j].actStatus === 1 &&
                        that.Sendact[j].unpassId !== '' &&
                        that.Sendact[j].unpassId !== null) {
                        that.unpassDel(that.Sendact[j])
                      } else if (
                        that.Sendact[j].actStatus === 2 &&
                        that.Sendact[j].unpassId !== '' &&
                        that.Sendact[j].unpassId !== null) {
                        that.unpassUpdate(that.Sendact[j])
                        that.IsExamine = 0
                      } else if (
                        that.Sendact[j].actStatus === 2 &&
                        (that.Sendact[j].unpassId === '' ||
                        that.Sendact[j].unpassId === null)
                      ) {
                        that.unpassSend(that.Sendact[j])
                        that.IsExamine = 0
                      }
                      that.UpdateAct(that.Sendact[j])
                    }
                    if (that.IsExamine === 0) {
                      that.sendInfo.sendStatus = 2
                    } else if (that.IsExamine === 1) {
                      that.sendInfo.sendStatus = 1
                    }
                    that.updateSend()
                  // that.Sendact = []
                  } else {
                    that.$message({
                      message: '当前服务器繁忙，请稍后再试',
                      type: 'warning'
                    })
                  }
                } else if (data === 2) {
                  this.$message('请继续编辑')
                }
              })
              .catch(() => {
                this.$message({
                  type: 'info',
                  message: '已取消审核'
                })
              // that.Sendact = []
              })
          }
        } else if (that.timeType === 2) {
          that.$message({
            message: '未到开放时间',
            type: 'warning'
          })
        } else if (that.timeType === 3) {
          that.$message({
            message: '已过开放时间',
            type: 'warning'
          })
        } else {
          that.$message({
            message: '系统繁忙请联系管理员',
            type: 'warning'
          })
        }
      })
    },
    // 看大图
    lookBigImage(index, indexs) {
      var that = this
      var images2 = []
      for (let i = 0; i < that.Sendact[index].images.length; i++) {
        images2.push(that.Sendact[index].images[i].imageUrl)
      }
      return images2
    }
  }
}
</script>
<style lang="scss">
.from-infomations {
  margin-left: 1rem;
  margin-right: 1.5rem;
}
.sendInfoImage {
  width: 10rem;
  height: 10rem;
}
.sendInfoImage1 {
  margin-left: 1rem;
  width: 10rem;
  height: 10rem;
}
.img-list {
  ul {
    display: flex;
    padding: 0 0 0 0;
    list-style-type: none;
    li {
      max-width: 10rem;
      max-height: 8.5rem;
      padding-right: 0.1rem;
      border-right: 1px solid #ecf0f1;
      flex-shrink: 0;
      img {
        width: 100%;
        max-height:8rem;
        object-fit:contain;
      }
    }
  }
}

</style>
